<?php
namespace app\controller\admin\user;

use app\BaseController;
use app\common\repositories\info\UserIntegralRepository;
use app\common\repositories\member\MemberRepository;
use app\common\repositories\user\UserRepository as repository;
use think\App;
use think\exception\ValidateException;

class User extends BaseController{

    protected $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function lst()
    {
        [$page,$limit] = $this->page();
        $where = $this->request->params(['keyword','phone']);
        return success($this->repository->getList($where,$page,$limit));
    }


    public function addIntegral()
    {
        $uid = input('uid');
        if(!$this->repository->get($uid))
            throw new ValidateException('用户不存在');
        $count = input('count',0);
        app()->make(UserIntegralRepository::class)->addIntegral($uid,$count,'后台增加数量');
        return success("增加成功");
    }


    public function setProxy()
    {
        $uid = input('uid');
        if(!$user = $this->repository->get($uid))
            throw new ValidateException('用户不存在');

        $level = input('level',0);
        $user->save(['member'=>$level]);


        return success('设置成功');
    }


    public function setMember()
    {
        $uid = input('uid');
        $status = input('status',1) == 0 ? 0 : 1;

        if(!$user = $this->repository->get($uid))
            throw new ValidateException('用户不存在');

        $user->save(['course_member'=>$status]);
        return success('设置成功');
    }



    public function detail($id)
    {
        return success($this->repository->get($id));
    }


}